/**
 * BootstrapingMiddleware.java
 * ************************** 
 * @date Apr 26, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package gossipServices.bootstraping.theBootstrapingService;

import java.util.Queue;

import gossipServices.basic.nodeDescriptors.NodeDescriptor;
import gossipServices.basic.view.PartialView;
import gossipServices.gossipMessages.BootstrapingConcreteMessage;
import gossipServices.gossipMessages.BootstrapingMessage;
import gossipServices.gossipThread.GossipThread;
import gossipServices.gossipThread.GossipThreadsInstructions;
import gossipServices.network.NetworkServicesAPI;
import gossipServices.pss.PeerSamplingServiceAPI;

/**
 * Description: 
 *
 */
public class BootstrapingMiddleware extends GossipThread {
	
	protected static LeafSet leafSet;
	protected static PrefixTable prefixTable;
	protected NetworkServicesAPI nsa;
	protected PeerSamplingServiceAPI pss;


	/**
	 * @param name
	 * @param cycleLenghtMsec
	 * @param view2
	 */
	public BootstrapingMiddleware(String name, long cycleLenghtMsec, PartialView view,
			PeerSamplingServiceAPI pss, NetworkServicesAPI nsa) {
		super(name, cycleLenghtMsec, view);
		this.pss = pss;
		this.nsa = nsa;		
	}
	
	/**
	 * 
	 * @return
	 */
	protected NodeDescriptor selectPeer(){
		return leafSet.selectPeer();
	}
	
	/**
	 * The key component of the bootstraping algorithm.
	 * It's responsible for generating a set of NodeDescriptor(s).
	 * It first takes the union of the LeafSet, 'cr' random 
	 * samples from the PeerSamplingService, the current PrefixTable
	 * and its own NodeDescriptor. It orders this set according to
	 * the distance from the peer node, and keeps the first
	 * 'c' entries.
	 * @param nd
	 * @return
	 */
	protected BootstrapingMessage createMessage(NodeDescriptor nd){
		//TODO
		return null;
	}
	
	/**
	 * 
	 * @param msgq
	 * @return
	 */
	protected boolean updateLeafSet(BootstrapingMessage msgq){
		//TODO
		Queue<NodeDescriptor> nodes = msgq.getCarryingNodeDescriptor();
		leafSet.merge(nodes);
		leafSet.sort();
		leafSet.adjustSuccPrec();
		return false;
	}
	
	/**
	 * 
	 * @param msgq
	 * @return
	 */
	protected boolean updatePrefixTable(BootstrapingMessage msgq){
		//TODO
		return false;
	}

}
